import axios, {
    type AxiosResponse,
    type AxiosRequestConfig,
    type AxiosRequestHeaders,
} from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';
import pinia from '../stores/index';
// import { useUserInfoStore } from "../stores/userInfo";

/* 定义response对象的data接口 */
interface ResponseData<T> {
    code: number;
    data: T;
    message: string;
}

// 配置新建一个 axios 实例
const service = axios.create({
    // 自动根据开发或生产环境来设置baseURL
    baseURL: 'https://www.dance365.com/apis',
    // baseURL: "//gmall-h5-api.atguigu.cn",
    timeout: 50000,
});

// 添加请求拦截器
service.interceptors.request.use((config: AxiosRequestConfig) => {
    return config;
});

// 添加响应拦截器
service.interceptors.response.use(
    /* 约束一下response */
    async (response: AxiosResponse<ResponseData<any>, any>) => {
        return response.data as any;
    },
    error => {
        // 对响应错误做点什么
        return Promise.reject(error);
    }
);

export default service;
